package com.huawei.util;
import java.sql.*;

public class DBUtil {
    public static String url="jdbc:mysql://localhost:3306/mytest";
    public static String driver = "com.mysql.jdbc.Driver";
    //获取驱动
    static {
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取数据源
     * @return
     */
    public static Connection getConnection(){
        try {
            return DriverManager.getConnection(url,"root","root");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 关闭数据方法
     * @param rs
     * @param st
     * @param conn
     */
    public static void close(ResultSet rs,Statement st,Connection conn){
        if (rs!=null){
            try {
                st.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (st!=null){
            try {
                st.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn!=null){
            try {
                st.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }


    public static void main(String[] args) {
        //测试是否能获取到数据库数据源
        Connection conn  = getConnection();
        System.out.println(conn);
    }
}
